package com.lanhe.versionRec.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.utils.SecurityUtils;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.versionRec.domain.LhVersionRec;
import com.lanhe.versionRec.mapper.LhVersionRecMapper;
import com.lanhe.versionRec.service.ILhVersionRecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * app版本表(LhVersionRec)表服务实现类
 *
 * @author zhanggl
 * @since 2024-01-13 21:56:45
 */
@Service
public class LhVersionRecServiceImpl extends ServiceImpl<LhVersionRecMapper, LhVersionRec> implements ILhVersionRecService {

	@Autowired
	private LhVersionRecMapper lhVersionRecMapper;

	/**
	 * 分页查询
	 *
	 * @param versionRec 实体
	 * @return java.util.List<com.lanhe.weightAssistant.domain.LhWeightAssistant>
	 * @author zhanggl
	 * @date 2024/1/13
	 */
	@Override
	public List<LhVersionRec> selectVersionRecList(LhVersionRec versionRec) {

		LambdaQueryWrapper<LhVersionRec> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(StringUtils.isNotBlank(versionRec.getVerNo()), LhVersionRec::getVerNo, versionRec.getVerNo());
		queryWrapper.eq(StringUtils.isNotBlank(versionRec.getVerSdkNo()), LhVersionRec::getVerSdkNo, versionRec.getVerSdkNo());
		queryWrapper.ge(ObjectUtil.isNotNull(versionRec.getBeginTime()), LhVersionRec::getCreateTime, versionRec.getBeginTime());
		queryWrapper.le(ObjectUtil.isNotNull(versionRec.getEndTime()), LhVersionRec::getCreateTime, versionRec.getEndTime());
		queryWrapper.eq(LhVersionRec::getDelFlag, Constants.GeneralIntType.ZERO.toString());
		queryWrapper.orderByDesc(LhVersionRec::getVerId);
		return lhVersionRecMapper.selectList(queryWrapper);
	}

	/**
	 * 根据ID查信息
	 *
	 * @param verId 版本号ID
	 * @return com.lanhe.versionRec.domain.LhVersionRec
	 * @author zhanggl
	 * @date 2024/1/13
	 */
	@Override
	public LhVersionRec selectVersionRecById(Long verId) {
		return lhVersionRecMapper.selectById(verId);
	}

	/**
	 * 新增
	 *
	 * @param lhVersionRec 实体
	 * @return int
	 * @author zhanggl
	 * @date 2024/1/13
	 */
	@Override
	public int insertVersionRec(LhVersionRec lhVersionRec) {
		lhVersionRec.setDelFlag(Constants.GeneralIntType.ZERO.toString());
		lhVersionRec.setCreateBy(SecurityUtils.getUsername());
		lhVersionRec.setCreateTime(new Date());
		return lhVersionRecMapper.insert(lhVersionRec);
	}

	/**
	 * 修改
	 *
	 * @param lhVersionRec 实体
	 * @return int
	 * @author zhanggl
	 * @date 2024/1/13
	 */
	@Override
	public int updateVersionRec(LhVersionRec lhVersionRec) {
		LambdaUpdateWrapper<LhVersionRec> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.eq(LhVersionRec::getVerId, lhVersionRec.getVerId());
		lhVersionRec.setUpdateBy(SecurityUtils.getUsername());
		lhVersionRec.setUpdateTime(new Date());
		return lhVersionRecMapper.update(lhVersionRec, updateWrapper);
	}

	/**
	 * 删除
	 *
	 * @param verId ID
	 * @return int
	 * @author zhanggl
	 * @date 2024/1/13
	 */
	@Override
	public int deleteVersionRecById(Long verId) {
		LambdaUpdateWrapper<LhVersionRec> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.eq(LhVersionRec::getVerId, verId)
				.set(LhVersionRec::getDelFlag, Constants.GeneralIntType.TWO.toString());

		return lhVersionRecMapper.update(null, updateWrapper);
	}

	/**
	 * 获取最新版本
	 *
	 * @return com.lanhe.versionRec.domain.LhVersionRec
	 * @author zhanggl
	 * @date 2024/1/13
	 */
	@Override
	public LhVersionRec getLatestVersion() {
		LambdaQueryWrapper<LhVersionRec> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhVersionRec::getDelFlag, Constants.GeneralIntType.ZERO.toString())
				.orderByDesc(LhVersionRec::getVerId)
				.last("limit 1");
		return lhVersionRecMapper.selectOne(queryWrapper);
	}
}

